package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.dao.GoodsMapper;
import com.ctgu.fastleader.service.*;
import com.ctgu.fastleader.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.*;

@Controller
@RequestMapping("/stock")
public class StockController {

    @Resource
    OutputOrderService outputOrderService;

    @Resource
    InputOrderService inputOrderService;

    @Resource
    GoodsSeatService goodsSeatService;

    @Resource
    GoodsService goodsService;

    @Resource
    WareHouseService wareHouseService;

    @Resource
    ShelfService shelfService;

    @Resource
    OutputRecordService outputRecordService;



    //查询所有仓库名
    @ResponseBody
    @RequestMapping("/findAllWarehouseName")
    public List<String> findAllWarehouseName(){
        return wareHouseService.findAllWarehouseName();
    }

    //查询所选仓库所有片区
    @ResponseBody
    @RequestMapping("/findAllPianQuByWareHouseName")
    public List<String> findAllPianQuByWareHouseName(String housename){
        System.out.println(housename);
        //找到仓库号
        Integer houseid = wareHouseService.selectHouseIdByHouseName(housename);
        List<String> shelfid = shelfService.findAllShelfIdByHouseId(houseid);
        List<String> pianqu = new ArrayList<>();
        //分割得到片区
        for(String el : shelfid){
            StringTokenizer str = new StringTokenizer(el,"-");
            pianqu.add(str.nextToken());
        }
        //去重
        HashSet<String> removal = new HashSet<>(pianqu);
        pianqu.clear();
        pianqu.addAll(removal);
        return pianqu;
    }

    //查询所选片区所有货架
    @ResponseBody
    @RequestMapping("/findAllHuoJiaByHouseIdAndPianQu")
    public List<String> findAllHuoJiaByHouseIdAndPianQu(String housename,String pianqu){
        //找到仓库号
        Integer houseid = wareHouseService.selectHouseIdByHouseName(housename);
        List<String> shelfid = shelfService.findAllShelfIdByHouseIdAndPianQu(houseid,pianqu);
        List<String> huojia = new ArrayList<>();
        //分割得到货架
        for(String el : shelfid){
            StringTokenizer str = new StringTokenizer(el,"-");
            str.nextToken();
            huojia.add(str.nextToken());
        }
        //去重
        HashSet<String> removal = new HashSet<>(huojia);
        huojia.clear();
        huojia.addAll(removal);
        return huojia;
    }

    //查询所选片区所有货架号
    @ResponseBody
    @RequestMapping("/findAllHuoJiaHaoByHouseIdAndPianQuAndHuoJia")
    public List<String> findAllHuoJiaHaoByHouseIdAndPianQuAndHuoJia(String housename,String pianqu,String huojia){
        //找到仓库号
        Integer houseid = wareHouseService.selectHouseIdByHouseName(housename);
        String pianquhuojia = pianqu + "-" + huojia;
        List<String> shelfid = shelfService.findAllShelfIdByHouseIdAndPianQuAndHuojia(houseid,pianquhuojia);
        List<String> huojiahao = new ArrayList<>();
        //分割得到货架
        for(String el : shelfid){
            StringTokenizer str = new StringTokenizer(el,"-");
            str.nextToken();
            str.nextToken();
            huojiahao.add(str.nextToken());
        }
        //去重
        HashSet<String> removal = new HashSet<>(huojiahao);
        huojiahao.clear();
        huojiahao.addAll(removal);
        return huojiahao;
    }


    //加载所有未出库商城订单
    @ResponseBody
    @RequestMapping("/allOutputOrderNotOutput")
    public List<OutputOrder> getAllOutputOrderNotOutput(){
        return outputOrderService.findAllOutputOrderNotOutput();
    }

    //加载所有未入库补货订单
    @ResponseBody
    @RequestMapping("/allInputOrderNotInput")
    public List<InputOrder> getAllInputOrderNotInput(){
        return inputOrderService.findAllInputOrderNotInput();
    }

    //进入入库页面
    @RequestMapping("/toInput")
    public String toInput(Integer orderid, Integer goodsid, Integer goodsnum, Model model){
        model.addAttribute("orderid",orderid);
        model.addAttribute("goodsid",goodsid);
        model.addAttribute("goodsnum",goodsnum);
        return "forward:/page/toInput";
    }

    //入库操作
    @RequestMapping("/input")
    public String input(HttpServletRequest request){
        //获取参数
        Integer orderid = Integer.parseInt(request.getParameter("orderid"));
        Integer goodsid = Integer.parseInt(request.getParameter("goodsid"));
        Integer goodsnum = Integer.parseInt(request.getParameter("goodsnum"));
        String housename = request.getParameter("housename");
        String pianqu = request.getParameter("pianqu");
        String huojia = request.getParameter("huojia");
        String huojiahao = request.getParameter("huojiahao");
        String shelfid = pianqu + "-" + huojia + "-" + huojiahao;

        //查询仓库
        Integer houseid = wareHouseService.selectHouseIdByHouseName(housename);

        //修改入库单
        Date inputDate = new Date(System.currentTimeMillis());
        InputOrder inputOrder = new InputOrder(orderid,1,inputDate,houseid,shelfid);
        inputOrderService.updateByPrimaryKeySelective(inputOrder);

        //修改商品位置表
        GoodsSeatKey goodsSeatKey = new GoodsSeatKey(goodsid,houseid,shelfid);
        GoodsSeat goodsSeat = goodsSeatService.selectByPrimaryKey(goodsSeatKey);
        if(goodsSeat == null){
            GoodsSeat goodsSeat1 = new GoodsSeat(goodsid,houseid,shelfid,goodsnum);
            goodsSeatService.insert(goodsSeat1);
            //此货架入库物品，状态修改为1
            Shelf shelf = new Shelf(shelfid,houseid,1);
            shelfService.updateByPrimaryKeySelective(shelf);
        }else{
            goodsSeat.setSeatnum(goodsSeat.getSeatnum()+goodsnum);
            goodsSeatService.updateByPrimaryKey(goodsSeat);
        }

        //修改商品总数量
        Goods goods = goodsService.selectByPrimaryKey(goodsid);
        goods.setGoodsnum(goods.getGoodsnum()+goodsnum);
        goodsService.updateByPrimaryKeySelective(goods);

        return "redirect:/page/toInwarehouse";
    }


    //进入出库页面
    @RequestMapping("/toOutput")
    public String toOutput(Integer orderid,Integer goodsid,Integer num,Model model){
        model.addAttribute("orderid",orderid);
        model.addAttribute("goodsid",goodsid);
        model.addAttribute("num",num);
        return "forward:/page/toOutput";
    }

    //显示出库位置
    @ResponseBody
    @RequestMapping("/showGoodsSeat")
    public List<GoodsSeat> showGoodsSeat(HttpServletRequest request){
        Integer goodsid = Integer.parseInt(request.getParameter("goodsid"));
        return goodsSeatService.findAllGoodsInSeat(goodsid);
    }

    //出库
    @ResponseBody
    @RequestMapping("/output")
    public String output(HttpServletRequest request,String[] houseid,String[] shelfid,String[] seatnum, String[] choosenum){
        Integer orderid = Integer.parseInt(request.getParameter("orderid"));
        Integer goodsid = Integer.parseInt(request.getParameter("goodsid"));
        Integer neednum = Integer.parseInt(request.getParameter("neednum"));
        System.out.println(orderid+"...."+goodsid+"....");
        List<GoodsSeat> goodsSeats = new ArrayList<>();
        for (int i = 0; i < houseid.length; i++){
            GoodsSeat goodsSeat = new GoodsSeat(goodsid,Integer.parseInt(houseid[i]),shelfid[i],Integer.parseInt(seatnum[i]));
            goodsSeats.add(goodsSeat);
        }
        List<Integer> deleteNum = new ArrayList<>();
        for (String s : choosenum) {
            deleteNum.add(Integer.parseInt(s));
        }

        //记录出库时间
        Date outputDate = new Date(System.currentTimeMillis());
        for(int i = 0; i < goodsSeats.size(); i++){
            //增加出库记录
            OutputRecord outputRecord = new OutputRecord(orderid,goodsid,goodsSeats.get(i).getHouseid(),goodsSeats.get(i).getShelfid(),deleteNum.get(i),outputDate);
            outputRecordService.insertSelective(outputRecord);

            //修改商品位置表
            int changedNum = goodsSeats.get(i).getSeatnum()-deleteNum.get(i);
            if(changedNum == 0){
                goodsSeatService.deleteByPrimaryKey(goodsSeats.get(i));
                //如果此货架无存放物品，状态修改为0
                if(goodsSeatService.findAllRecordByShelfIdAndHouseId(goodsSeats.get(i).getShelfid(),goodsSeats.get(i).getHouseid()).isEmpty()){
                    Shelf shelf = new Shelf(goodsSeats.get(i).getShelfid(),goodsSeats.get(i).getHouseid(),0);
                    shelfService.updateByPrimaryKeySelective(shelf);
                }
            }else{
                goodsSeats.get(i).setSeatnum(changedNum);
                goodsSeatService.updateByPrimaryKey(goodsSeats.get(i));
            }
        }

        //修改商品总数量
        Goods goods = goodsService.selectByPrimaryKey(goodsid);
        goods.setGoodsnum(goods.getGoodsnum()-neednum);
        goodsService.updateByPrimaryKeySelective(goods);

        //修改商家订单状态
        OutputOrder outputOrder = new OutputOrder(orderid,1);
        outputOrderService.updateByPrimaryKeySelective(outputOrder);


        return "123";
    }


//    查询入库记录
    @ResponseBody
    @RequestMapping("/searchInputorderrecord")
    public List<InputOrder> searchInputorderrecord(){
        return inputOrderService.findAllInputOrderRecord();
    }


    //    查询出库记录
    @ResponseBody
    @RequestMapping("/searchoutputorderrecord")
    public List<OutputRecord> searchoutputorderrecord(){
        return outputRecordService.findAllInputOrderRecord();
    }
}
